<template>
  <div class="login-container">
    <!-- 登录 -->
    <div class="login-wrap">
      <div class="login">
        <div class="ligniform">
          <div class="content">
            <el-form class="el-form" :model="loginParams" :rules="loginRules" ref="formRef">
              <el-form-item class="input-text clearFix">
                <span></span>
                <el-input type="text" v-model="loginParams.phone" placeholder="邮箱/用户名/手机号" />
              </el-form-item>
              <el-form-item class="input-text clearFix">
                <span class="pwd"></span>
                <el-input type="text" v-model="loginParams.password" placeholder="请输入密码" />
              </el-form-item>
              <button class="btn" @click.prevent="login">登&nbsp;&nbsp;录</button>
            </el-form>
            <div class="call clearFix">
              <router-link class="register" to="/register">立即注册</router-link>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reqLogin } from '@/api/login'
import router from '@/router'
import useUserInfoStore from '@/stores/userInfo'
import type { ILoginParams } from '@/types/login'
import { setToken } from '@/utils/token-utils'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'

// 实例化仓库
const store = useUserInfoStore()

// const {getUserInfo} = storeToRefs(store)
const loginParams = ref<ILoginParams>({
  phone: '',
  password: ''
})

// 兜底验证引用
const formRef = ref<FormInstance>()

// 登录验证
const loginRules = reactive<FormRules<ILoginParams>>({
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    {
      pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
      message: '手机号码不正确',
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\d{6}$/,
      message: '账号或密码有误',
      trigger: 'blur'
    }
  ]
})

const getIn = () => {
  store.getInfo()
}

// 登录
const login = async () => {
  // 兜底验证
  formRef.value?.validate()

  const res = await reqLogin(loginParams.value)

  // console.log(res)

  if (res.code === 200) {
    ElMessage.success('登陆成功')

    const { token, nickName } = res.data
    // token存本地
    sessionStorage.setItem('token', token)
    setToken(token)
    
    // 存储用户信息
    store.getUserInfo(token, nickName)
    // 跳转
    router.replace('/')
  }
}

onMounted(() => {
  getIn()
})
</script>

<script lang="ts">
export default {
  name: 'LoginView'
}
</script>

<style lang="scss" scoped>
.login-container {
  .login-wrap {
    height: 487px;
    background-color: #e93854;

    .login {
      width: 1200px;
      height: 487px;
      margin: 0 auto;
      background: url(./images/loginbg.png) no-repeat;
    }

    .ligniform {
      width: 420px;
      height: 306px;
      box-sizing: border-box;
      background: #fff;
      float: right;
      top: 45px;
      position: relative;
      padding: 20px;

      .tab {
        li {
          width: 50%;
          float: left;
          text-align: center;

          a {
            width: 100%;
            display: block;
            height: 50px;
            line-height: 50px;
            font-size: 20px;
            font-weight: 700;
            color: #333;
            border: 1px solid #ddd;
            box-sizing: border-box;
            text-decoration: none;
          }

          .current {
            border-bottom: none;
            border-top-color: #28a3ef;
            color: #e1251b;
          }
        }
      }
      .content {
        width: 380px;
        height: 316px;
        box-sizing: border-box;
        border-top: none;
        padding: 18px;

        .el-form {
          margin: 15px 0 18px 0;
          font-size: 12px;
          line-height: 18px;

          span {
            float: left;
            width: 37px;
            height: 32px;
            border: 1px solid #ccc;
            background: url(./images/icons.png) no-repeat -10px -201px;
            box-sizing: border-box;
            border-radius: 2px 0 0 2px;
          }

          .el-input {
            width: 302px;
          }

          .setting {
            label {
              float: left;
            }

            .forget {
              float: right;
            }
          }

          .btn {
            background-color: #e1251b;
            padding: 6px;
            border-radius: 0;
            font-size: 16px;
            font-family: 微软雅黑;
            word-spacing: 4px;
            border: 1px solid #e1251b;
            color: #fff;
            width: 100%;
            height: 36px;
            margin-top: 25px;
            outline: none;
          }
        }

        .call {
          margin-top: 30px;

          ul {
            float: left;

            li {
              float: left;
              margin-right: 5px;
            }
          }

          .register {
            float: right;
            font-size: 15px;
            line-height: 38px;
          }

          .register:hover {
            color: #4cb9fc;
            text-decoration: underline;
          }
        }
      }
    }
  }

  .copyright {
    width: 1200px;
    margin: 0 auto;
    text-align: center;
    line-height: 24px;

    ul {
      li {
        display: inline-block;
        border-right: 1px solid #e4e4e4;
        padding: 0 20px;
        margin: 15px 0;
      }
    }
  }
}
</style>
